//
//  LZCreatStaffMemberViewController.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/3/2.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZCreatStaffMemberViewController.h"
#import "UIViewController+NavigationItemSetting.h"
#import "LZStaffTextCell.h"
#import "LZStaffSelectAccessCell.h"
#import "LZManagerStaffPresenter.h"
#import "LZStaffModel.h"
#import "LZAlertView.h"
#import "extobjc.h"
#import "LZRemindView.h"

static NSString *textCell = @"textcell";
static NSString *accessCell = @"accesscell";
static NSString *passwordTemp = @"*&^$*&))";

@interface LZCreatStaffMemberViewController ()<UITableViewDelegate,UITableViewDataSource,LZStaffTextFiledCellDelegate,LZStaffSelectAccessCellDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) LZManagerStaffPresenter *staffPresenter;
@property (strong, nonatomic) UIButton *saveBtn;

@end

@implementation LZCreatStaffMemberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configureUI];
    
    [self configureNavBar];
}

#pragma mark -
#pragma mark UI 
- (void)configureUI
{
    if (self.isEditStaff) {
        self.title = LZLocalizedString(@"staff_edit_staff_member", nil);
    }else{
        self.title = LZLocalizedString(@"create_staff_member", nil);
    }
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LZStaffTextCell" bundle:nil] forCellReuseIdentifier:textCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"LZStaffSelectAccessCell" bundle:nil] forCellReuseIdentifier:accessCell];
    
    _staffPresenter = [[LZManagerStaffPresenter alloc] init];
    if (!_staffModel) {
        _staffModel = [[LZStaffModel alloc] init];    
    }else{
        self.staffModel.password = passwordTemp;
    }
}

- (void)configureNavBar
{
    self.saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.saveBtn.frame = CGRectMake(0, 0, 40, 40);
    self.saveBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    self.saveBtn.userInteractionEnabled = NO;
    [self.saveBtn setTitleColor:UIColorFromRGB(0x666666, 1) forState:UIControlStateNormal];
    [self.saveBtn setTitle:LZLocalizedString(@"save", nil) forState:UIControlStateNormal];
    [self.saveBtn addTarget:self action:@selector(saveStaff) forControlEvents:UIControlEventTouchUpInside];
    [self navigationItemSetting:@[self.saveBtn] type:NAVIGATIONITEMSETTING_RIGHT];
}


#pragma mark -
#pragma mark priva event
- (void)saveStaff
{
    @weakify(self);
    if (self.isEditStaff) { //如果是编辑
        if (self.staffModel.password.length > 0 && self.staffModel.password.length < 6) {
            LZAlertView *alert = [[LZAlertView alloc] initWithContent:LZLocalizedString(@"enter_password_6_chart", nil) andImage:nil cancelButtonTitle:LZLocalizedString(@"confirm", nil) otherButtonTitles:nil];
            [alert show];
            return;
        }
        
        if ([self.staffModel.password isEqualToString:passwordTemp]) {
            self.staffModel.password = nil;
        }
        [self.staffPresenter editStaffWithModel:self.staffModel Success:^(id data) {
            @strongify(self);
            if (self.EditOrAddStaffSuccessBlock) {
                self.EditOrAddStaffSuccessBlock();
            }
            [self.navigationController popViewControllerAnimated:YES];
            
        } Failure:^(NSString *fail) {
            LZAlertView *alert = [[LZAlertView alloc] initWithContent:LZLocalizedString(@"submit_fail", nil) andImage:nil cancelButtonTitle:LZLocalizedString(@"confirm", nil) otherButtonTitles:nil];
            [alert show];
        }];
    }else{ //新加员工
       [self.staffPresenter addNewStaffWithModel:self.staffModel Success:^(id data) {
           @strongify(self);
           if (self.EditOrAddStaffSuccessBlock) {
               self.EditOrAddStaffSuccessBlock();
           }
           [self.navigationController popViewControllerAnimated:YES];
       } Failure:^(NSString *fail) {
           if ([fail isEqualToString:@"0400014"]) {//昵称已存在
               [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"] andContent:LZLocalizedString(@"staff_account_have", @"")];
           }else{
               LZAlertView *alert = [[LZAlertView alloc] initWithContent:fail andImage:nil cancelButtonTitle:LZLocalizedString(@"confirm", nil) otherButtonTitles:nil];
               [alert show];
           }
           
       }];
    }
}


/**
 每次编辑操作, 检查按钮状态
 */
- (void)chnageSaveState
{
    self.saveBtn.userInteractionEnabled = NO;
    [self.saveBtn setTitleColor:UIColorFromRGB(0x666666, 1) forState:UIControlStateNormal];
    if (self.staffModel.staffAccounts.length == 0) {
        return;
    }
    
    if (!self.isEditStaff) {
        if (self.staffModel.password.length < 6) {
            return;
        }
    }
    if (self.staffModel.staffName.length == 0) {
        return;
    }
    
    if (self.staffModel.role != 1 && self.staffModel.role != 2 && self.staffModel.role != 3) {
        return;
    }
    
    [self.saveBtn setTitleColor:UIColorFromRGB(0xffffff, 1) forState:UIControlStateNormal];
    self.saveBtn.userInteractionEnabled = YES;
    
}
#pragma mark -
#pragma mark cell delegate
- (void)textFieldText:(NSString *)text indexPath:(NSIndexPath *)indexP
{
    if (indexP.row == 0) {
        self.staffModel.staffAccounts = text;
    }else if (indexP.row == 1){
        self.staffModel.password = text;
    }else if (indexP.row == 2){
        self.staffModel.staffName = text;
    }
    [self chnageSaveState];
}

- (void)selectAccessIndex:(NSInteger)accessNum
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    self.staffModel.role = accessNum;
    [self chnageSaveState];
}

#pragma mark -
#pragma mark tableview delegage
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 3) {
        return 130;
    }
    return 98;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:{
            LZStaffTextCell *cell = [tableView dequeueReusableCellWithIdentifier:textCell];
            cell.staffTextFieldCellDelegate = self;
            [cell loadCellName:LZLocalizedString(@"staff_account", nil) textFieldPlaceholder:LZLocalizedString(@"enter_staff_member_account", nil) textFieldText:self.staffModel.staffAccounts indexPath:indexPath];
            cell.isEditStaff = self.isEditStaff;
            
            return cell;
        }
            break;
        case 1:{
            LZStaffTextCell *cell = [tableView dequeueReusableCellWithIdentifier:textCell];
            cell.staffTextFieldCellDelegate = self;
            [cell loadCellName:LZLocalizedString(@"staff_password", nil) textFieldPlaceholder:LZLocalizedString(@"enter_staff_member_password", nil) textFieldText:self.staffModel.password indexPath:indexPath];
            
            return cell;
        }
            break;
        case 2:{
            LZStaffTextCell *cell = [tableView dequeueReusableCellWithIdentifier:textCell];
            cell.staffTextFieldCellDelegate = self;
            [cell loadCellName:LZLocalizedString(@"staff_name", nil) textFieldPlaceholder:LZLocalizedString(@"enter_staff_member_name", nil) textFieldText:self.staffModel.staffName indexPath:indexPath];
            
            return cell;
        }
            break;
        case 3:{
            LZStaffSelectAccessCell *cell = [tableView dequeueReusableCellWithIdentifier:accessCell];
            cell.staffSelectAccessCellDelegate = self;
            [cell loadData:self.staffModel];
            
            return cell;
        }
            break;
            
        default:
            break;
    }
    return nil;
}


@end
